package com.wasion.pwportable.protocol.analyzer.terminal;

import com.wasion.pwportable.protocol.Protocol;
import com.wasion.pwportable.protocol.analyzer.AbstractAnalyzer;

public class REG_STATUS_RD_Analyzer extends AbstractAnalyzer {

	@Override
	public void dataAnalysis(String ackBS) {

		String dataType = ackBS.substring(DATATPYESTARTBIT, DATATPYENDBIT);

		String data = ackBS.substring(DATABITSTART, DATABITSTART + 32);

		if (dataType.equalsIgnoreCase(Protocol.REG_STATUS_RD)) {
			result.setRegStatus(getRegister(data));
			result.setCommPhase(getRegPhase(data));
			result.setSignalQuality(getSignal(data));

			result.setCurrentTime(getCurrentTime(data));
			result.setHeartBeat(getHeartCycle(data));

			if (data.substring(20, 32).contains("EE")) {
				result.setLastHeartTime("Not Available");
			} else
				result.setLastHeartTime(getlastHeart(data));
		}
	}

	private String getCurrentTime(String data) {
		// 当前终端时间
		String time = data.substring(6, 18);

		// long s = Long.parseLong(time);
		// return DateTimeUtil.longToDate(s);
		return readTime(time);

	}

	private int getHeartCycle(String data) {
		// 心跳周期
		String cycle = data.substring(18, 20);
		return Integer.parseInt(cycle, 16);
	}

	private String getlastHeart(String data) {
		// 最后心跳时间
		String time = data.substring(20, 32);

		return readTime(time);
	}

	private String getRegister(String data) {
		// 注册状态
		String resStatus = data.substring(0, 2);
		return resStatus;
	}

	private String getRegPhase(String data) {
		// 注册阶段
		String regPhase = data.substring(2, 4);
		return regPhase;
	}

	private String getSignal(String data) {
		// 信号质量 dbm
		String signal = data.substring(4, 6);
		return signal;
	}
}
